﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Narwhunderful.Aphelia.Framework.Graphics;
using Narwhunderful.Aphelia.Framework.Input;
using Narwhunderful.Aphelia.Framework.Input.Keyboard;

namespace Narwhunderful.Aphelia.Framework.UI
{

    public class UIManager : DrawableComponent, IDesignableComponent
    {
        public event EventHandler<EventArgs> DesigningChanged = null;

        bool _isDesigning = true;
        public virtual bool IsDesigning
        {
            get
            {
                return _isDesigning;
            }
            internal set
            {
                _isDesigning = value;
            }
        }

        InputManager _inputManager;
        public InputManager InputManager
        {
            get
            {
                return _inputManager;
            }
            protected internal set
            {
                _inputManager = value;
            }
        }

        public KeyboardManager KeyboardManager
        {
            get
            {
                return _inputManager.KeyboardManager;
            }
        }

        HUD _hud = null;
        public HUD HUD
        {
            get
            {
                return _hud;
            }
            set
            {
                _hud = value;
            }
        }

        DrawingContext2D _drawingContext;
        public DrawingContext2D DrawingContext2D
        {
            get
            {
                return _drawingContext;
            }
            set
            {
                _drawingContext = value;
            }
        }

        RasterizerState _rasterizerState = new RasterizerState();
        public RasterizerState RasterizerState
        {
            get
            {
                return _rasterizerState;
            }
        }

        public UIManager(Game game)
            : base(game)
        {
            _rasterizerState.ScissorTestEnable = true;
            _drawingContext = new Graphics.DrawingContext2D(game.GraphicsDevice);
            _inputManager = new Input.InputManager(game);
            _inputManager.KeyboardManager.AddKeys(new Keys[] {
                Keys.Down, 
                Keys.Left, 
                Keys.Right, 
                Keys.Up, 
                Keys.A, 
                Keys.B, 
                Keys.C, 
                Keys.D, 
                Keys.E, 
                Keys.F, 
                Keys.G, 
                Keys.H, 
                Keys.I, 
                Keys.J, 
                Keys.K, 
                Keys.L, 
                Keys.M, 
                Keys.N, 
                Keys.O, 
                Keys.P, 
                Keys.Q, 
                Keys.R, 
                Keys.S, 
                Keys.T, 
                Keys.U, 
                Keys.V, 
                Keys.W, 
                Keys.X, 
                Keys.Y, 
                Keys.Z, 
                Keys.D0, 
                Keys.D1, 
                Keys.D2, 
                Keys.D3, 
                Keys.D4, 
                Keys.D5, 
                Keys.D6, 
                Keys.D7, 
                Keys.D8, 
                Keys.D9, 
                Keys.Back, 
                Keys.Space, 
                Keys.OemPlus, 
                Keys.OemMinus, 
                Keys.OemOpenBrackets, 
                Keys.OemCloseBrackets, 
                Keys.OemQuestion,
                Keys.OemPeriod,
                Keys.OemComma, 
                Keys.OemPipe, 
                Keys.OemSemicolon, 
                Keys.OemQuotes, 
                Keys.OemTilde,
            });
            _hud = new HUD(this);
        }

        protected override void InternalUpdate(GameTime gameTime)
        {
            base.InternalUpdate(gameTime);
            _inputManager.Update(gameTime);
            _hud.Update(gameTime);
        }

        protected override void InternalDraw()
        {
            base.InternalDraw();
            _hud.Draw();
        }

        public virtual void BeginDesigning()
        {
            if (!_isDesigning)
            {
                _isDesigning = true;
                _hud.BeginDesigning();
                OnDesigningChanged(this, new EventArgs());
            }
        }

        public virtual void EndDesigning()
        {
            if (_isDesigning)
            {
                _isDesigning = false;
                _hud.EndDesigning();
                OnDesigningChanged(this, new EventArgs());
            }
        }

        protected internal virtual void OnDesigningChanged(object sender, EventArgs e)
        {
            if (DesigningChanged != null) DesigningChanged(sender, e);
        }

    }

}
